package com.ytjj.qmyx.admin.service.data.impl;

import com.ytjj.qmyx.admin.dao.OrderStatisticsMapper;
import com.ytjj.qmyx.admin.model.data.response.ChannelCostMoneyResponse;
import com.ytjj.qmyx.admin.model.data.response.ChannelStatisticResponse;
import com.ytjj.qmyx.admin.model.request.dataStatistics.AreaTimePeriodRequest;
import com.ytjj.qmyx.admin.model.response.statistics.AllAreaTimePeriodStatisticsResponse;
import com.ytjj.qmyx.admin.model.response.statistics.AreaCostMoneyResponse;
import com.ytjj.qmyx.admin.model.response.statistics.AreaTimePeriodResponse;
import com.ytjj.qmyx.admin.service.data.AreaTimePeriodService;
import com.ytjj.qmyx.admin.utils.CalendarUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @ClassName AreaTimePeriodServiceImpl
 *
 * 
 * @Version 1.0
 **/

@Service
public class AreaTimePeriodServiceImpl implements AreaTimePeriodService {
    @Resource
    private OrderStatisticsMapper orderStatisticsMapper;

    @Override
    public AllAreaTimePeriodStatisticsResponse getAreaTimePeriodStatisticsList(AreaTimePeriodRequest areaTimePeriodRequest) {
        AllAreaTimePeriodStatisticsResponse allAreaTimePeriodStatisticsResponse = new AllAreaTimePeriodStatisticsResponse();
        Date startTime = new Date();
        Date endTime = new Date();
        if(StringUtils.isNotBlank(areaTimePeriodRequest.getStartTime())&&areaTimePeriodRequest.getStartTime().length()==10){
            startTime = CalendarUtil.dateFormatParse(LocalDate.parse(areaTimePeriodRequest.getStartTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }else{
            startTime = CalendarUtil.getStartTime(new Date());
        }
        if(StringUtils.isNotBlank(areaTimePeriodRequest.getEndTime())&&areaTimePeriodRequest.getEndTime().length()==10){
            endTime =  CalendarUtil.getEndTime(CalendarUtil.dateFormatParse(LocalDate.parse(areaTimePeriodRequest.getEndTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd"))));
        }else{
            endTime = CalendarUtil.getEndTime(new Date());
        }
        //省份数据集合,其他属性
        List<AreaTimePeriodResponse> provinceStatisticsOthers = orderStatisticsMapper.getProvinceStatisticsOthers(areaTimePeriodRequest.getChannel(), startTime, endTime);
        Map<String, List<AreaTimePeriodResponse>> provinceMap = provinceStatisticsOthers.stream().collect(Collectors.groupingBy(AreaTimePeriodResponse::getProvince));
        AreaTimePeriodResponse areaTimePeriodResponse=orderStatisticsMapper.getTotalStatistics(areaTimePeriodRequest.getChannel(),startTime,endTime);
        //总支付金额
        BigDecimal payAmountTotal = provinceStatisticsOthers.stream().map(AreaTimePeriodResponse::getPayAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        //总退款金额
        BigDecimal refundAmountTotal = provinceStatisticsOthers.stream().map(AreaTimePeriodResponse::getRefundAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
        areaTimePeriodResponse.setPayAmount(payAmountTotal);
        areaTimePeriodResponse.setRefundAmount(refundAmountTotal);
        areaTimePeriodResponse.setGrossProfit(payAmountTotal.subtract(refundAmountTotal));
        //总成本
        BigDecimal totalCostMoney = orderStatisticsMapper.getTotalCostMoneyStatisticsList(areaTimePeriodRequest.getChannel(), startTime, endTime);
        areaTimePeriodResponse.setCostMoney(totalCostMoney);
        areaTimePeriodResponse.setNetProfit(areaTimePeriodResponse.getGrossProfit().subtract(totalCostMoney));

        allAreaTimePeriodStatisticsResponse.setChannelStatisticsSummary(areaTimePeriodResponse);
        allAreaTimePeriodStatisticsResponse.setHourStatisticsSummary(areaTimePeriodResponse);
        allAreaTimePeriodStatisticsResponse.setProvinceStatisticsSummary(areaTimePeriodResponse);
        //渠道集合数据
        allAreaTimePeriodStatisticsResponse.setChannelStatisticsList(orderStatisticsMapper.getChannelStatisticsList(areaTimePeriodRequest.getChannel(),startTime,endTime));
        //省份数据集合
        List<AreaTimePeriodResponse> provinceStatisticsList = orderStatisticsMapper.getProvinceStatisticsList(areaTimePeriodRequest.getChannel(), startTime, endTime);

        List<AreaCostMoneyResponse> areaCostMoneyList = orderStatisticsMapper.getProvinceCostMoneyStatisticsList(areaTimePeriodRequest.getChannel(), startTime, endTime);

        provinceStatisticsList.stream().forEach(e->{
            List<AreaTimePeriodResponse> areaTimePeriodResponses = provinceMap.get(e.getProvince());
            if(!CollectionUtils.isEmpty(areaTimePeriodResponses)){
                AreaTimePeriodResponse entity = areaTimePeriodResponses.get(0);
                e.setPayAmount(entity.getPayAmount());
                e.setRefundAmount(entity.getRefundAmount());
                e.setGrossProfit(entity.getPayAmount().subtract(entity.getRefundAmount()));
                // 成本
                BigDecimal costMoney = BigDecimal.ZERO;
                if (!org.springframework.util.CollectionUtils.isEmpty(areaCostMoneyList)) {
                    List<AreaCostMoneyResponse> collect = areaCostMoneyList.stream().filter(item2 -> item2.getProvince().equals(e.getProvince())).collect(Collectors.toList());
                    costMoney = org.springframework.util.CollectionUtils.isEmpty(collect) ? BigDecimal.ZERO : collect.get(0).getCostMoney();
                }
                e.setCostMoney(costMoney);
                e.setNetProfit(e.getGrossProfit().subtract(costMoney));
            }
        });
        allAreaTimePeriodStatisticsResponse.setProvinceStatisticsList(provinceStatisticsList);
        //时间数据集合
        allAreaTimePeriodStatisticsResponse.setHourStatisticsList(orderStatisticsMapper.getHourStatisticsList(areaTimePeriodRequest.getChannel(),startTime,endTime));
        return allAreaTimePeriodStatisticsResponse;
    }
}